<template>
  <div class="people">
    <p class="detail-tip">
      基本信息<span v-if="showView" @click="toggleView">编辑</span>
    </p>
    <div class="com-container" v-if="showView">
      <div class="avator">
        <img :src="comInfo.logo" />
      </div>
      <p class="company">{{ comInfo.cname }}</p>
      <div class="line">
        <p class="pie">
          <span>企业规模:</span>
          <span>{{ comInfo.size_data }}</span>
        </p>
        <p class="pie">
          <span>所属行业:</span>
          <span>{{ comInfo.industry_data }}</span>
        </p>
        <p class="pie">
          <span>企业性质:</span>
          <span>{{ comInfo.attr_data }}</span>
        </p>
        <p class="pie">
          <span>手机号:</span>
          <span>{{ comInfo.phone }}</span>
        </p>
        <p class="pie">
          <span>邮箱:</span>
          <span>{{ comInfo.email }}</span>
        </p>
        <p class="pie">
          <span>纳税人识别号:</span>
          <span>{{ comInfo.business_number }}</span>
        </p>
        <p class="pie">
          <span>企业注册地址:</span>
          <span v-if="comInfo.city_name">【{{ comInfo.province_name }}/{{ comInfo.city_name }}】</span>
          <span>{{ comInfo.address }}</span>
        </p>
      </div>
    </div>
    <div class="com-form" v-else>
      <div class="upload">
        <div class="img">
          <img v-if="comInfo.logo" :src="comInfo.logo" />
          <p class="seat" v-else @click="dialogVisible = true">logo</p>
          <input
            ref="imgfile"
            type="file"
            @change="uploadImg($event)"
            accept="image/jpeg,image/jpg,image/png"
            class="hidden"
          />
        </div>
        <p class="btn" @click="dialogVisible = true">上传logo</p>
        <p class="tips">支持 5M 以内 jpg、png 格式的图片</p>

        <avatar-cropper :dialog-visible.sync="dialogVisible" @closeAvatarDialog="uploadImg1"></avatar-cropper>
      </div>
      <div class="postForm">
        <div class="form-item">
          <div class="text" required>企业名称:</div>
          <div class="input">
            <input
              v-model="comInfo.cname"
              type="text"
              placeholder="请输入企业名称"
            />
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>所属行业:</div>
          <div class="select">
            <client-only>
              <el-select
                v-model="comInfo.industryid"
                placeholder="选择所属行业"
                @change="changeInc"
              >
                <el-option
                  v-for="item in industryList"
                  :key="'industryList_' + item.id"
                  :label="item.tagname"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </client-only>
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>企业性质:</div>
          <div class="select">
            <client-only>
              <el-select
                v-model="comInfo.attrid"
                placeholder="选择企业性质"
                @change="changeAttr"
              >
                <el-option
                  v-for="item in typeList"
                  :key="'typeList_' + item.id"
                  :label="item.tagname"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </client-only>
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>企业规模:</div>
          <div class="select">
            <client-only>
              <el-select
                v-model="comInfo.sizeid"
                placeholder="选择企业规模"
                @change="changeScale"
              >
                <el-option
                  v-for="item in scaleList"
                  :key="'scaleList_' + item.id"
                  :label="item.tagname"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </client-only>
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>纳税人识别号:</div>
          <div class="input">
            <input
              v-model="comInfo.business_number"
              type="text"
              placeholder="请输入纳税人识别号"
            />
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>手机号码:</div>
          <div class="input">
            <input
              v-model="comInfo.phone"
              type="text"
              placeholder="请输入手机号码"
            />
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>邮箱:</div>
          <div class="input">
            <input
              v-model="comInfo.email"
              type="text"
              placeholder="请输入邮箱"
            />
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>企业注册地址:</div>
          <div style="display: block">
            <el-cascader
              size="small"
              :options="options"
              v-model="comInfo.locat"
              @change="handleChange">
            </el-cascader>
          </div>

          <div class="textarea">
            <textarea
              v-model="comInfo.address"
              placeholder="请输入企业注册详细地址"
            />
          </div>
        </div>


        <div class="submit">
          <button class="confirm" type="button" @click="submit">保存</button>
          <button class="cancel" type="button" @click="cancel">取消</button>
        </div>

      </div>
    </div>
  </div>
</template>

<script>
import { provinceAndCityData, CodeToText, TextToCode} from 'element-china-area-data'
import { uploadFile } from '@/api/users';
import avatarCropper from '@/components/avatarCropper/avatarCropper.vue';
export default {
  components: {
    avatarCropper
  },
  props: ['info', 'inc', 'sizes', 'atts'],
  data () {
    return {
      comInfo: {},
      showView: true,
      logo: '',
      industryList: [],
      typeList: [],
      scaleList: [],
      options: provinceAndCityData,
      dialogVisible: false
    }
  },
  watch: {
    info (val) {
      this.comInfo = val;
    },
    inc (val) {
      this.industryList = val;
    },
    sizes (val) {
      this.scaleList = val;
    },
    atts (val) {
      this.typeList = val;
    },
  },
  created () {
    this.comInfo = this.info;
  },
  mounted () {
  },
  methods: {
    handleChange (value) {
      this.comInfo.province_name = CodeToText[value[0]];
      this.comInfo.city_name = CodeToText[value[1]];
      this.comInfo.province = value[0];
      this.comInfo.city = value[1];
    },
    changeInc (data) {
      this.comInfo.industryid = data;
      const curOne = this.industryList.find(s => s.id === data) || {};
      this.comInfo.industry_data = curOne.tagname || '';
    },
    changeAttr (data) {
      this.comInfo.attrid = data;
      const curOne = this.typeList.find(s => s.id === data) || {};
      this.comInfo.attr_data = curOne.tagname || '';
    },
    changeScale (data) {
      this.comInfo.sizeid = data;
      const curOne = this.scaleList.find(s => s.id === data) || {};
      this.comInfo.size_data = curOne.tagname || '';
    },
    toggleView () {
      this.showView = false;
    },
    uploadImg (e) {
      const file = e.target.files[0];
      if (!file) { return }
      // KB单位,可以获取图片的大小，到时候可以根据图片大小进行选择上传
      // const imgSize = file.size / 1024 / 1024;
      const fileType = file.type;

      const reader = new FileReader();

      reader.onloadend = data => {
        if (/^image\[jpeg|png|gif]/.test(fileType)) {
          console.log(this.result);
        }
        // 图片的 base64 格式, 可以直接当成 img 的 src 属性值
        // const dataURL = reader.result;
        // this.comInfo.logo = dataURL;
        // this.logo = file;
        const formData = new FormData();
        formData.append('file', file);
        uploadFile(formData).then(res => {
          if (res.code === 200) {
            this.comInfo.logo = res.data.link;
            this.$message.success('上传成功！');
          } else {
            this.$message.error(res.msg)
          }
        })
      }
      // console.log(file);
      reader.readAsDataURL(file);
    },
    uploadImg1 (file) {
      this.dialogVisible = false;
      if (!file) { return }
      // KB单位,可以获取图片的大小，到时候可以根据图片大小进行选择上传
      // const imgSize = file.size / 1024 / 1024;
      const fileType = file.type;
      const reader = new FileReader();

      reader.onloadend = data => {
        if (/^image\[jpeg|png|gif]/.test(fileType)) {
          console.log(this.result);
        }
        const file2 = new File([file], 'logo.jpg', {type: fileType});
        // 图片的 base64 格式, 可以直接当成 img 的 src 属性值
        // const dataURL = reader.result;
        // this.comInfo.logo = dataURL;
        // this.logo = file;
        const formData = new FormData();
        formData.append('file', file2);
        uploadFile(formData).then(res => {
          if (res.code === 200) {
            this.comInfo.logo = res.data.link;
            this.$message.success('上传成功！');
          } else {
            this.$message.error(res.msg)
          }
        })
      }
      // console.log(file);
      reader.readAsDataURL(file);
    },
    upload () {
      this.$refs.imgfile.click()
    },
    cancel () {
      this.showView = true;
    },
    submit () {
      this.showView = true;
      // this.$emit('submit_com', this.logo)
    },
  }
}
</script>
<style lang="scss" scoped>

.people {
  position: relative;
  .avator {
    text-align: center;
    font-size: 0;
    height: 180px;
    width: 180px;
    margin: 10px auto;
    overflow: hidden;
    border: 1px solid #ebebeb;
    border-radius: 50%;
    img {
      height: 100%;
    }
  }
  .company {
    font-size: 26px;
    color: #24292f;
    text-align: center;
    margin-bottom: 30px;
  }
  .line {
    padding-left: 70px;
    line-height: 22px;
    .pie {
      display: inline-block;
      position: relative;
      padding-left: 8px;
      margin-right: 20px;
      margin-bottom: 15px;
      min-width: 60px;
      span {
        font-size: 16px;
        &:first-child {
          color: #999;
          margin-right: 5px;
        }
        &:last-child {
          color: #24292f;
        }
      }
      &::before {
        content: '';
        position: absolute;
        top: 3px;
        left: 0;
        width: 2px;
        height: 16px;
        background: $base-color;
      }
    }
  }
  .com-form {
    padding: 30px 0;
    .upload {
      float: left;
      width: 350px;
      text-align: center;
      .img {
        height: 180px;
        width: 180px;
        margin: 0 auto;
        line-height: 180px;
        border: 1px solid #ebebeb;
        background: #ebebeb;
        border-radius: 50%;
        overflow: hidden;
        img {
          height: 100%;
          vertical-align: middle;
        }
        .seat {
          height: 100%;
          width: 100%;
          margin: 0;
          color: #999;
          cursor: pointer;
        }
      }
      .btn {
        color: #999;
        margin-top: 10px;
        &:hover {
          color: $base-color;
          cursor: pointer;
        }
      }
      .tips {
        font-size: 12px;
        color: #999;
        margin-top: 20px;
      }
    }
    .postForm {
      padding-left: 350px;
      .form-item {
        margin-bottom: 10px;
        line-height: 40px;
        & > div {
          display: inline-block;
        }
        & > .text {
          position: relative;
          padding-left: 20px;
          min-width: 120px;
          color: #24292f;
          vertical-align: top;
          &[required] {
            &::before {
              content: '*';
              position: absolute;
              left: 0px;
              top: 0px;
              color: $base-color-danger;
            }
          }
        }
        .input {
          input {
            line-height: 28px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 0 10px;
            width: 230px;
            font-size: 16px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
        }
        .label {
          label {
            margin-right: 30px;
          }
        }
        .select {
          ::v-deep .el-input__inner {
            width: 230px;
            height: 30px;
            line-height: 30px;
            border: 1px solid #ccc;
            font-size: 16px;
            color: #24292f;
            padding-right: 10px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
          &.small {
            ::v-deep .el-input__inner {
              width: 103px;
              padding-right: 20px;
              margin-right: 20px;
            }
            ::v-deep .el-input__suffix {
              right: 20px;
            }
          }
        }
        .textarea {
          margin-top: 10px;
          textarea {
            resize: none;
            height: 140px;
            width: 360px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 10px;
            color: #24292f;
            font-size: 16px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
          .count {
            text-align: right;
            span {
              color: $base-color;
              margin: 0 5px;
            }
          }
        }
      }
      .submit {
        margin-top: 60px;
        padding-left: 30px;
        text-align: left;
        button {
          height: 45px;
          width: 150px;
          margin: 0 20px;
          border: 1px solid $base-color;
          border-radius: 6px;
          font-size: 16px;
          &:hover {
            box-shadow: 0 0 6px 0 $base-color;
          }
        }
        .cancel {
          background: #fff;
          color: $base-color;
        }
        .confirm {
          background: $base-color;
          color: #fff;
        }
      }
    }
  }
}
</style>
